package work20241206_chap7.m_demo10;

/**
 * @author zdl
 * @date 2024/12/5
 */
public class Bank {
    public Account openAccount(long id, String password, int type){
        if (type == 1){
            SavingAccount savingAccount = new SavingAccount(id, 0, password, 0.001);
            System.out.println("创建一个储蓄账户SavingAccount");
            return savingAccount;
        } else if (type == 0) {
            Account account = new Account(id, 0, password);
            System.out.println("创建一个Account 账户");
            return account;
        } else if (type == 2) {
            CreditAccount creditAccount = new CreditAccount(id, 0, password, 2000);
            System.out.println("创建一个信用账户CreditAccount");
            return creditAccount;
        }
        return null;
    }

    public double deposit(Account a, double amount){
        double s = 0;
        s = a.getBalance() + amount;
        a.setBalance(s);
        return a.getBalance();
    }
    public double withdraw(Account a, double amount){
        if (amount > a.getBalance()) {
            if (a instanceof CreditAccount) {
                CreditAccount creditAccount = (CreditAccount) a;
                if ((creditAccount.getCreditLine() + a.getBalance()) < amount) {
                    System.out.println("信用卡额度不足");
                    return a.getBalance();
                }
                return (creditAccount.getCreditLine() + a.getBalance()) - amount;
            }
            if (a instanceof SavingAccount) {
                System.out.println("余额不足！");
                return a.getBalance(); //钱不够取
            }
        }
        return a.getBalance() - amount;
    }
}
